// 这里封装登录相关的请求异步函数
import { ApiRes, LoginForm, Token } from "@/types/data"
import { RootAction, RootThunkAction } from "@/types/store"
import http from "@/utils/request"
import { setToken } from "@/utils/token"

// 登录请求函数
export const login = (values: LoginForm): RootThunkAction => {
    return async(dispatch) => {
        const res = await http.post<ApiRes<Token>>('/authorizations', values)
        console.log(res)
        // 1. 存储到仓库
        dispatch({
            type: 'login/login',
            payload: res.data.data
        } as RootAction)
        // 2. 存储到本地
        setToken(res.data.data)
    }
}

// 发送验证码
export const getCode = (mobile: string): RootThunkAction => {
    return async(dispatch) => {
        await http.get(`/sms/codes/${mobile}`)
    }
}